const express = require('express');
const router = express.Router();
// 引入刚才定义的表
const userModel = require('./../models/user');
const groupModel = require('./../models/group');
const response = require('../utils/response')
const { auth, SECRET } = require('../utils/auth')

/**
 * 创建group的api
 * @route POST /group/create
 * @summary 创建group
 * @group group - 组注释
 * @param {Routers.model} router.body - group's info
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 * @security JWT
 */
router.post('/create', auth, async (req, res) => {

  let data = req.body;

  let result = await groupModel.create({ groupname: data.groupname, routers: data.routers });
  response(res, 0, "OK", result);
});

/**
 * 创建group的api
 * @route GET /group/findall
 * @summary 查看group
 * @group group - 组注释
 * @returns {object} 200 - An array of user info
 * @returns {Error}  default - Unexpected error
 * @security JWT
 */
router.get('/findall', auth, async (req, res) => {

  let result = await groupModel.find({}, { _id: 0, createDate: 0 });
  response(res, 0, "OK", result);
});

/**
 * @typedef Routers
 * @property {string} groupname - groupname
 * @property {array} routers - username
 */

module.exports = router;
